﻿namespace Acoris.Media
{
    /// <summary>
    /// 描述一个平台无关的颜色值
    /// </summary>
    public struct Color
    {
        private byte m_alpha;
        private byte m_red;
        private byte m_green;
        private byte m_blue;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="red"></param>
        /// <param name="green"></param>
        /// <param name="blue"></param>
        public Color(byte red,byte green,byte blue) : this(red, green, blue, 255) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="red"></param>
        /// <param name="green"></param>
        /// <param name="blue"></param>
        /// <param name="alpha"></param>
        public Color(byte red, byte green, byte blue,byte alpha)
        {
            m_alpha = alpha;
            m_red = red;
            m_green = green;
            m_blue = blue;
        }

        /// <summary>
        /// 获取或设置透明通道值
        /// </summary>
        public byte A {
            get { return m_alpha; }
            set { m_alpha = value; }
        }

        /// <summary>
        /// 获取或设置红色通道值
        /// </summary>
        public byte R {
            get { return m_red; }
            set { m_red = value; }
        }
        
        /// <summary>
        /// 获取或设置绿色通道值
        /// </summary>
        public byte G {
            get { return m_green; }
            set { m_green = value; }
        }

        /// <summary>
        /// 获取或设置蓝色通道值
        /// </summary>
        public byte B {
            get { return m_blue; }
            set { m_blue = value; }
        }

        #region Override

        /// <summary>
        /// 获取当前对象与指定对象之间的等价性
        /// </summary>
        /// <param name="obj">指定要比较的对象</param>
        /// <returns>如果两个对象之间等价则返回true，否则返回false</returns>
        public override bool Equals(object obj)
        {
            if (obj is Color x) {
                var y = this;
                return x.m_red == y.m_red &&
                       x.m_green == y.m_green &&
                       x.m_blue == y.m_blue &&
                       x.m_alpha == y.m_alpha;
            }
            return false;
        }

        /// <summary>
        /// 获取当前对象的哈希值
        /// </summary>
        /// <returns>返回当前对象的哈希值</returns>
        public override int GetHashCode()
        {
            return (m_red.GetHashCode() ^ m_green.GetHashCode()) ^
                   (m_blue.GetHashCode() ^ m_alpha.GetHashCode());
        }


        /// <summary>
        /// 获取与当前对象等价的字符串形式
        /// </summary>
        /// <returns>返回与当前对象等价的字符串形式</returns>
        public override string ToString()
        {
            return $"r:{m_red},g:{m_green},b:{m_blue},a:{m_alpha}";
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public
#if NET_UWP
        Windows.UI.Color
#else
        object
#endif
        GetColor()
        {
#if NET_UWP
            return Windows.UI.Color.FromArgb(m_alpha, m_red, m_green, m_blue);
#else
            return null;
#endif
        }

        public static bool operator !=(Color x,Color y)
        {
            return x.m_red != y.m_red ||
                   x.m_green != y.m_green ||
                   x.m_blue != y.m_blue ||
                   x.m_alpha != y.m_alpha;
        }

        public static bool operator ==(Color x,Color y)
        {
            return x.m_red == y.m_red && 
                   x.m_green == y.m_green &&
                   x.m_blue == y.m_blue &&
                   x.m_alpha == y.m_alpha;
        }

    }
}
